<template>
  <div class="question_main">
    <div class="main" style="padding-bottom: 0;">
      <div class="patient-form">
        <div class="form">
          <div class="dxd-input bordered">
            <div class="dxd-input-title">患者姓名</div>
            <input v-model="patient.patient_info.name" type="text" placeholder="请填写真实姓名" maxlength="15" class="dxd-input-input">
            </div>
          <div class="dxd-input bordered">
            <div class="dxd-input-title">性别</div>
            <div class="input-tags">
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.sex==0"> 男
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.sex=0"> 男
              </div>
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.sex==1" > 女
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.sex=1"> 女
              </div>
            </div></div>
          <div class="dxd-picker">
            <div class="dxd-picker-cell">
              <div class="dxd-input bordered">
                <div class="dxd-input-title">出生年月</div>
                <input v-model="patient.patient_info.birthday" type="date" placeholder="请选择出生年月" maxlength="-1"
                       class="dxd-input-input"/></div>
            </div></div>
          <div class="dxd-input bordered">
            <div class="dxd-input-title">体重(kg)</div>
            <input v-model="patient.patient_info.weight" type="number" placeholder="请输入体重" maxlength="-1" max="250"
                   class="dxd-input-input"></div>
          <div class="checkbox-textarea">
            <div class="dxd-input">
              <div class="dxd-input-title">过敏史</div>
              <div class="input-tags">
                <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.allergy_history==0">
                  无
                </div>
                <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.allergy_history=0">
                  无
                </div>
                <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.allergy_history==1">
                  有
                </div>
                <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.allergy_history=1">
                  有
                </div>
              </div></div>
            <div class="cell-textarea" style="display: none;">
              <div class="textarea"><textarea id="body" v-model="patient.patient_info.allergy_history_content" name="textInput"
                                                                 placeholder="请填写过敏药物（非常重要）"
                                                                 required="required" maxlength="20"
                                                                 class="textarea__input needsclick"
                                                                 style="height: 130px; color: rgb(51, 51, 51); max-height: 500px; font-size: 17px;"></textarea>
                <div class="textarea__tip">
                  <div class="textarea__counter"><span class="">0/20</span></div>
                </div>
              </div>
            </div>
          </div>
          <div class="checkbox-textarea">
            <div class="dxd-input">
              <div class="dxd-input-title">过往病史</div>
              <div class="input-tags">
                <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.illness_history==0">
                  无
                </div>
                <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.illness_history=0">
                  无
                </div>
                <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.illness_history==1">
                  有
                </div>
                <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.illness_history=1">
                  有
                </div>
              </div></div>
            <div class="cell-textarea1" style="display: none;">
              <div class="textarea"><textarea id="body" v-model="patient.patient_info.illness_history_content" name="textInput" placeholder="请填写过往病史"
                                                                 required="required" maxlength="50"
                                                                 class="textarea__input needsclick"
                                                                 style="height: 130px; color: rgb(51, 51, 51); max-height: 500px; font-size: 17px;"></textarea>
                <div class="textarea__tip">
                  <div class="textarea__counter"><span class="">0/50</span></div>
                </div>
              </div>
            </div>
          </div>
          <div class="dxd-input bordered">
            <div class="dxd-input-title">肝功能</div>
            <div class="input-tags">
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.liver_function==0">
                正常
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.liver_function=0">
                正常
              </div>
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.liver_function==1">
                异常
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.liver_function=1">
                异常
              </div>
            </div></div>
          <div class="dxd-input bordered">
            <div class="dxd-input-title">肾功能</div>
            <div class="input-tags">
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.renal_function==0">
                正常
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.renal_function=0">
                正常
              </div>
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.renal_function==1">
                异常
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.renal_function=1">
                异常
              </div>
            </div></div>
          <div class="dxd-input bordered">
            <div class="dxd-input-title">孕育情况</div>
            <div class="input-tags">
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.pregnancy_status==0"> 无
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.pregnancy_status=0"> 无
              </div>
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.pregnancy_status==1">
                怀孕中
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.pregnancy_status=1">
                怀孕中
              </div>
              <div class="input-tags-item dxd-tag checked normal" v-if="patient.patient_info.pregnancy_status==2">
                哺乳中
              </div>
              <div class="input-tags-item dxd-tag normal" v-else @click="patient.patient_info.pregnancy_status=2">
                哺乳中
              </div>
            </div></div>
        </div>
        <div class="operation">
          <button class="dxd-button dxd-button-primary feedback" @click="save_patient"> 保存并选择
          </button>
          <div class="operation-delete" v-if="patient.type==2" @click="delPatient()">删除</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, watch, onMounted} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import { useRouter, useRoute} from "vue-router";
import patient from "../../api/patient.js";
const router = useRouter()
const route = useRoute()

watch(
    ()=> patient.patient_info.allergy_history,
    ()=>{
      const element = document.querySelector('.cell-textarea');
      if (patient.patient_info.allergy_history==1) {
        element.style.display = 'block';
      } else if (patient.patient_info.allergy_history==0) {
        patient.patient_info.allergy_history_content = ""
        element.style.display = 'none';
      }
    }
)

watch(
    ()=> patient.patient_info.illness_history,
    ()=>{
      const element = document.querySelector('.cell-textarea1');
      if (patient.patient_info.illness_history==1) {
        element.style.display = 'block';
      } else if (patient.patient_info.illness_history==0) {
        patient.patient_info.illness_history_content = ""
        element.style.display = 'none';
      }
    }
)


onMounted(()=>{
  patient.type = route.query.type
  if (patient.type == 1){
    patient.patient_info= {
      name: "",
          sex: null,
          birthday: "",
          weight: null,
          allergy_history: null,
          allergy_history_content: "",
          illness_history: null,
          illness_history_content: "",
          liver_function: null,
          renal_function: null,
          pregnancy_status: null
    }
  } else if (patient.type ==  2){
    patient.patient_id = route.query.id
    patient.get_patient_info_by_id()
  }
})


const save_patient=()=>{
  if (patient.type == 1) {
    let token = sessionStorage.access_token || localStorage.access_token;
    let payload = token.split(".")[1]  // 载荷
    let payload_data = JSON.parse(atob(payload)) // 用户信息
    patient.patient_info.user = payload_data.user_id
    patient.add_patient(token)

    if (patient.addFlag = true) {
      ElMessage.success("患者信息添加成功！")
      let token = sessionStorage.access_token || localStorage.access_token;
      patient.get_patients_info(token)
      router.push('/patient')
    }
  } else if (patient.type == 2) {
    let token = sessionStorage.access_token || localStorage.access_token;
    patient.edit_patient(token)
    if (patient.editFlag = true) {
      ElMessage.success("患者信息修改成功！")
      let token = sessionStorage.access_token || localStorage.access_token;
      patient.get_patients_info(token)
      router.push('/patient')
    }
  }
}

const delPatient=()=>{
  patient.del_patient()
  if (patient.delFlag = true) {
    ElMessage.success("患者信息删除成功")
    let token = sessionStorage.access_token || localStorage.access_token;
    patient.get_patients_info(token)
    router.push('/patient')
  }
}
</script>

<style scoped>
.question_main {
  background-color: #f4f4f4;
  height: 1022px;
}

.main {
  width: 768px;
  height: 1022px;
  margin: 0 auto;
}

.patient-form {
  min-height: 100vh;
  max-height: 1022px;
  background-color: #fff;
}
.dxd-input.bordered {
  position: relative;
}
.dxd-input {
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  min-height: 59px;
  padding: 0 16px;
  background: #fff;
}
.dxd-input-title {
  -webkit-box-flex: 0;
  flex: 0 0 102px;
  align-self: flex-start;
  margin-right: 14px;
  color: #333;
  font-size: 17px;
  line-height: 59px;
}
.dxd-input-input {
  display: block;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  color: #4d4d4d;
  font-weight: 300;
  font-size: 17px;
  line-height: 1.12;
  caret-color: #00c792;
}
input {
  -webkit-appearance: textfield;
}

button, input, textarea {
  border: none;
  outline: none;
}
button, input, textarea {
  font: inherit;
}
.dxd-input.bordered:after {
  position: absolute;
  right: 16px;
  bottom: 0;
  left: 16px;
  box-sizing: border-box;
  height: 1px;
  background-color: #ebebeb;
  transform: scaleY(.5);
  content: "";
}

.patient-form .input-tags {
  margin-right: -16px;
  padding-top: 13px;
}

.patient-form .input-tags-item:not(:last-child) {
  margin-right: 20px;
}
.patient-form .input-tags-item {
  min-width: 32px;
  margin-bottom: 13px;
  text-align: center;
}
.dxd-tag.normal {
  padding: 8px 12px;
}
.dxd-tag {
  display: inline-block;
  height: 16px;
  color: #4d4d4d;
  font-size: 14px;
  line-height: 16px;
  white-space: nowrap;
  background-color: #fafafa;
  border-radius: 4px;
}
.dxd-input-input:disabled {
  color: #4d4d4d;
  background-color: #fff;
  -webkit-opacity: 1;
  -webkit-text-fill-color: #4d4d4d;
}
.patient-form .operation {
  padding: 20px 16px 60px;
}
.dxd-button-primary {
  color: #fff;
  background-color: #00c792;
}
.dxd-button {
  position: relative;
  display: flex;
  -webkit-box-pack: center;
  justify-content: center;
  box-sizing: border-box;
  width: 100%;
  padding: 14px 0;
  overflow: hidden;
  font-weight: 500;
  font-size: 17px;
  line-height: 19px;
  text-align: center;
  border: 0;
  border-radius: 24px;
  outline: 0;
  appearance: none;
}
.dxd-button:after {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #1a1a1a;
  opacity: 0;
  content: "";
}
.dxd-tag.checked {
  color: #00c792;
  background-color: #e5f9f4;
}
.patient-form .cell-textarea {
  padding: 0 16px 20px;
  background-color: #fff;
}
.patient-form .cell-textarea1 {
  padding: 0 16px 20px;
  background-color: #fff;
}
.textarea {
  position: relative;
  width: 100%;
}
.textarea__input {
  display: block;
  width: 100%;
  color: #333;
  font-size: 14px;
  line-height: 20px;
  border: 0;
  resize: none;
}
.textarea__tip {
  position: relative;
  margin-top: 15px;
  font-size: 12px;
  line-height: 13px;
  text-align: right;
}
.textarea__counter {
  display: inline-block;
  color: #ccc;
}
.patient-form .operation-delete {
  padding-top: 20px;
  color: #666;
  font-size: 13px;
  line-height: 15px;
  text-align: center;
}
</style>